package com.parkcar;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import HttpUtils.HttpCouponRequest;
import Modle.CouponBean;
import adapter.CouponAdapter;
import ui.RefreshLayout;
import util.BaseActivity;
import util.BaseTools;
import util.BaseUtil;

/**
 * Created by Administrator on 2016/2/19.
 */
public class CouponActivity extends BaseActivity {
    private ListView mListView;
    private TextView ero_title,ero_content,ero_click;
    private RelativeLayout ero_ll;
    private RefreshLayout mRefresh;
    private int pageNum=1;
    private HttpCouponRequest couponRequest;
    private CouponAdapter mAdapter;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_base_list);
        setTitle("我的优惠券");
        initView();
    }
    private void initView(){
        mListView = (ListView) findViewById(R.id.mListView);
        mListView.setPadding(8,0,8,0);
        ero_title = (TextView) findViewById(R.id.ero_title);
        ero_content = (TextView) findViewById(R.id.ero_content);
        ero_click = (TextView) findViewById(R.id.ero_click);
        ero_ll = (RelativeLayout) findViewById(R.id.ero_ll);
        mRefresh = (RefreshLayout) findViewById(R.id.pull_ly);
        ero_title.setText("您还没有优惠券哦");
        ero_content.setText("参加系统活动，或者参加停车场所在商城活动均可获得对应的优惠券");
        ero_click.setVisibility(View.GONE);
        mAdapter = new CouponAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                toast("点击进入详情");
            }
        });
        mRefresh.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                pageNum++;
                getData();
            }
        });
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum=1;
                getData();
            }
        });
        getData();
    }
    @Override
    protected void updateUI(boolean isLogin) {

    }
    private void getData(){
        couponRequest = new HttpCouponRequest(this,0);
        couponRequest.setPageNum(pageNum);
        couponRequest.setmIHttpClinetListener(this);
        couponRequest.doRequest();
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        mRefresh.setLoading(false);
        mRefresh.setRefreshing(false);
        if(mBaseResult==null){return;}
        if(couponRequest!=null&&action.equalsIgnoreCase(couponRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0){
                List<CouponBean> mList = mBaseResult.toArray(CouponBean.class,"list");
                if(pageNum==1){
                    if(BaseTools.isValueEmpty(mList)){
                        ero_ll.setVisibility(View.VISIBLE);
                    }else{
                        ero_ll.setVisibility(View.GONE);
                        mAdapter.setData(mList);
                    }
                }else{
                    if(!BaseTools.isValueEmpty(mList)){
                        mAdapter.addData(mList);
                    }
                }
            }else{
                toast(mBaseResult.getStatus().getMsg());}
        }
    }
}
